<!DOCTYPE HTML>
<html class="x-admin-sm">
<head>
    <th:block th:insert="head/head :: common_head"></th:block>
</head>
<body onload="init('cdmc')" style="background-color: white">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card" style="margin: 10px;">
                    <div class="layui-tab layui-tab-brief">
                        <div class="layui-tab-content" >
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form layui-form-pane" autocomplete="off">
                                    <div class="layui-form-item" id="sjcd_div">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>上级菜单名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="hidden" id="pid" name="pid">
                                            <input type="text" id="pname" name="pname" class="layui-input" disabled="disabled">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>菜单名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="cdmc" name="cdmc" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            菜单链接：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="link" name="link" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            权限代码：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="qxdm" name="qxdm" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            图标名称：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="tbmc" name="tbmc" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">
                                            显示序号：
                                        </label>
                                        <div class="layui-input-block">
                                            <input type="text" id="xsxh" name="xsxh" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <button class="layui-btn" lay-submit="" lay-filter="*" id="bc">
                                            新增存盘
                                        </button>
                                        <button class="layui-btn layui-btn-warm" id="btn_qy" style="display: none" lay-event="qy">
                                            菜单迁移
                                        </button>
                                        <button class="layui-btn layui-btn-normal" id="btn_xj" style="display: none" lay-event="xj">
                                            新增下级
                                        </button>
                                        <button class="layui-btn layui-btn-danger" id="btn_sc" style="display: none" lay-event="sc">
                                            删除数据
                                        </button>
                                    </div>
                                    <div style="height: 25px;"></div>
                                    <div class="layui-form-item" id="jgqy_div" style="display: none;">
                                        <label class="layui-form-label">
                                            <span class='x-red'>*</span>选择新的上级菜单：
                                        </label>
                                        <div class="layui-inline layui-show-xs-block">
                                            <select name="new_pid" id="new_pid" lay-search="">
                                            </select>
                                        </div>
                                        <div class="layui-inline layui-show-xs-block">
                                            <button class="layui-btn layui-btn-normal" id="btn_qdqy" lay-event="qdqy">
                                                确定迁移
                                            </button>
                                        </div>
                                    </div>
                                </form>
                                <div style="height:100px;"></div>
                            </div>

                        </div>
                    </div>
                </div>
        </div>
    </div>
</div>
</div>
<th:block th:insert="head/head :: common_jsfiles"></th:block>
<script>
    /**
     * lx 0 表示新增  1 表示修改 id 为主键
     * 所有增改页面，载入时，都是通过doFirst回调renderForm
     *
     */
    var lx="[[${lx}]]";
    var id="[[${id}]]";
    var pid="[[${pid}]]";
    var url ="[[${serverUrl}]]/sys/menu/";
    var ms = "menu_info";
    $(document).ready(function() {
        if(lx=="1"){
            $("#bc").html("修改存盘");
        }else{
            $("#bc").html("新增存盘")
        }
    });

    //表单相关
    layui.use(['form'], function(){
        var form = layui.form;

        //渲染表格参数
        var renderForm = function (formData){
            console.info("formData:"+JSON.stringify(formData));

            //如果是修改，才渲染用于迁移
            if (lx==1){
                var options = '';
                options += '<option value="" selected="selected">请选择新的上级菜单</option>';
                formData.tMenuList.forEach(function(item) {
                    options += '<option value="' + item.id + '">' + item.name + '</option>';
                });
                $('select[id="new_pid"]').html(options); // 添加选项到 select 中
            }

            if (lx==1){
                if (is_null(formData.tMenu.pMenu)){
                    $("#sjcd_div").hide();
                }else{
                    $("#pname").val(formData.tMenu.pMenu.name);
                    $("#pid").val(formData.tMenu.pMenu.id);
                }
                if (formData.tMenu.jb=="1"){
                    $("#btn_xj").show();
                }
                if (formData.tMenu.jb=="2"){
                    $("#btn_qy").show();
                }
                $("#cdmc").val(formData.tMenu.name);
                $("#link").val(formData.tMenu.href);
                $("#tbmc").val(formData.tMenu.ico);
                $("#xsxh").val(formData.tMenu.xh);
                $("#qxdm").val(formData.tMenu.qxdm);
                $("#btn_sc").show();
            }else if (lx==0){
                if (!is_null(formData.pMenu)){
                    $("#sjcd_div").show();
                    $("#pid").val(formData.pMenu.id);
                    $("#pname").val(formData.pMenu.name);
                }else{
                    $("#sjcd_div").hide();
                }
            }
            form.render(); // 重新渲染
        }

        //表单数据初始化
        function doFirst()
        {
            //发送的请求
            var jsonData = {
                id: id,
                lx: lx,
                pid: pid,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_form(url+"info",JSON.stringify(sendStr),"数据加载中...",renderForm);
        }

        //提交逻辑
        function onSave(nonce_zhi){
            //获取提交令牌
            if (is_null(nonce_zhi)){
                $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
                layer.msg("没有获取到操作凭证！请重试！",{icon:2,anim:6});
                return false;
            }
            var jsonData = {
                pid: $("#pid").val(),
                cdmc: $("#cdmc").val(),
                link: $("#link").val(),
                tbmc: $("#tbmc").val(),
                xsxh: $("#xsxh").val(),
                xmlx: $("#xmlx").val(),
                qxdm: $("#qxdm").val(),
                lx: lx,
                id: id,
                nonce_ms:ms,
                nonce_zhi:nonce_zhi,
                timestamp:new Date().getTime()
            };
            var sendStr = {
                data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
            };
            post_json_token(url+"save",JSON.stringify(sendStr),"数据提交中...");
        }

        //监听提交
        form.on('submit(*)', function(data){
            if ($("#cdmc").val()==""){
                layer.msg("请输入菜单名称！",{icon:2,anim:6});
                init('cdmc');
                return false;
            }
            if (!confirm("确定提交存盘？")) return false;
            $('#bc').addClass("layui-btn-disabled").attr("disabled",true);
            psot_get_nonce(ms,"[[${serverUrl}]]/getnonce",onSave)
            return false;
        });

        // 为所有带有lay-event属性的元素添加事件监听
        $(document).on('click', '[lay-event]', function(obj){
            var eventName = $(this).attr('lay-event'); // 获取事件名称
            if(eventName === 'sc'){
                if (!confirm("确定删除菜单信息？")) return false;
                var jsonData = {
                    lx: lx,
                    id: id,
                    timestamp:new Date().getTime()
                };
                var sendStr = {
                    data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
                };
                post_json_token(url+"del",JSON.stringify(sendStr),"数据删除中...");
                return false;
            } else if(eventName === 'xj'){
                window.location.href="info?lx=0&id="+id+"&pid="+id+"";
                return false;
            } else if(eventName === 'qy'){
                $("#jgqy_div").show();
                return false;
            } else if(eventName === 'qdqy'){
                if($("#new_pid").val()==""){
                    layer.msg("没有选择新的上级菜单！",{icon:2,anim:6});
                    init_select('new_pid');
                    return false;
                }
                if (!confirm("确定变更上级？如果有下级，会一起迁移至新的上级！")) return false;
                var jsonData = {
                    lx: lx,
                    id: id,
                    new_pid: $("#new_pid").val(),
                    timestamp:new Date().getTime()
                };
                var sendStr = {
                    data:encrypt_str(JSON.stringify(jsonData),localStorage.getItem("a"),localStorage.getItem("b"))
                };
                post_json_token(url+"qy",JSON.stringify(sendStr),"数据迁移中...");
                return false;
            }
        });

        //首先执行
        doFirst();
    });

    //存盘之后的回调，涉及树形，略显繁琐
    function on_result(result)
    {
        $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
        if (result.code!=500)
        {
            if (result.code==1 || result.code==2){
                let rtn = decrypt_str(result.data,localStorage.getItem("a"),localStorage.getItem("b"));
                let cdmc = JSON.parse(rtn).name;
                //新增成功
                layer.msg(result.msg, { time: 1000 }, function() {
                    parent.init_tree(cdmc);
                    window.location.reload();
                });
            }else if (result.code==3){
                layer.msg(result.msg, { time: 1000 }, function() {
                    parent.init_tree();
                    parent.$("#menu_info").attr("src", "info?lx=0");
                });//失败时只提示信息，不关闭弹出层且不刷新父页
            }else{
                layer.msg(result.msg, { time: 1000 }, function() {
                });//失败时只提示信息，不关闭弹出层且不刷新父页
            }
        }
        else
        {
            layer.msg(result.msg);
        }
    }

</script>
</body>
</html>